/*
 * @Description: 支出   BillExpenditure
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-29 15:41:32
 * @LastEditors: ZhengZhuang
 * @LastEditTime: 2019-08-30 11:02:03
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/toast.dart';
import 'package:C20Driver/widget/no_data_widget.dart';
import 'package:C20Driver/widget/spinkit.dart';
import 'package:C20Driver/widget/wallet_list_component.dart';
import 'package:async/async.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class BillExpenditure extends StatefulWidget {
  @override
  _BillExpenditureState createState() => _BillExpenditureState();
}

class _BillExpenditureState extends State<BillExpenditure> {
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  int _page = 1;
  int _pageSize = 20;
  List _listAll = [];

  @override
  void initState() {
    super.initState();
  }

  /// 获取用户全部账单
  Future _getBillInfoAll() {
    return _memoizer.runOnce(() async {
      var list = await Request.getBillInfo('1', _page, _pageSize);
      setState(() {
        _listAll = list;
      });
      return list;
    });
  }

  /// 上拉加载
  Future _getBillPullupLoad() async {
    var list = await Request.getBillInfo('1', _page + 1, _pageSize);
    if (list.length != 0) {
      _listAll.addAll(list);
      _page++;
      setState(() {
        _listAll = _listAll;
        _page = _page;
      });
    } else {
      ToastX.center('暂无更多数据');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: FutureBuilder(
        future: _getBillInfoAll(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (_listAll.length == 0) return NoDataWidget();
            return EasyRefresh(
                footer: ClassicalFooter(
                  bgColor: Colors.white,
                  textColor: Colors.black,
                  noMoreText: '',
                  loadText: '下拉刷新',
                  loadReadyText: '松开刷新',
                  loadingText: '正在加载中',
                  loadedText: '加载完成',
                  showInfo: false,
                ),
                child: ListView(
                  padding: EdgeInsets.only(top: ScreenUtil().setWidth(32)),
                  children: _listAll
                      .map((item) => WalletListComponent(
                          direction: item['direction'],
                          title: item['remark'],
                          time: item['ytd'],
                          debitAmount: item['debit_amount'],
                          creditAmount: item['credit_amount']))
                      .toList(),
                ),
                onLoad: () => _getBillPullupLoad());
          } else {
            return Spinkit();
          }
        },
      ),
    );
  }
}
